
#include <thirdparty/fist-log.h>
#include <core/task/task_interface.h>

#include "db_manager.h"
#include "db_conf.h"
#include "db_mariadb.h"
#include "db.pb.h"

CDBManager::CDBManager()
        : m_cfg_parser(new CDBConfParser())
        , m_last_test(0)
{

}

CDBManager::~CDBManager()
{
    delete m_cfg_parser;
}

bool CDBManager::Init()
{
    if (!m_cfg_parser->Load("../config/server/core/db.xml")) {
        return false;
    }
    m_db = new CDBMariadb(this);
    m_db->Init();
    return true;
}

void CDBManager::Update()
{
    m_db->Tick();
    CHECK_TIMEOUT(m_last_test, 1000, DBTest);
}

void CDBManager::Destroy()
{
    m_db->Destroy();
}

const DBConf *CDBManager::conf()
{
    return m_cfg_parser->conf();
}

void CDBManager::DBTest()
{
    user people;
    people.set_id(rand());
    people.set_name(std::to_string(rand()));
    people.set_password(std::to_string(rand()));

    CMngPtr<ITaskManager> p_task(this);
    p_task->PushTask([this]()->cppcoro::task<> {
        co_await CoDBTest();
    });
}

cppcoro::task<> CDBManager::CoDBTest()
{
    user people;
    people.set_id(rand());
    people.set_name(std::to_string(rand()));
    people.set_password(std::to_string(rand()));
    int ret = co_await m_db->CoInsert(&people);
    LOG_DEBUG("ret-->{}", ret);
}
